package com.ssh;

import java.util.Arrays;

import org.junit.Test;

public class Solve_130 {
	private int m, n;
	private int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
	public void solve(char[][] board) {
		if (board == null || board.length == 0) {
			return;
		}
		n = board.length;//行
		m = board[0].length;//列
		char[][] t = new char[n][m];
		int result = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (board[i][j] == 'O') {
					exch(t, board);
					result = dfs(t, i, j);
					if (result < 0) {
						exch(board, t);
					}
				}
				
			}
		}
    }
	
	private void exch(char[][] x, char[][] y) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				x[i][j] = y[i][j];
			}
		}
	}
	
	private int dfs(char[][] board, int x, int y) {
		if (x < 0 || x >= n || y < 0 || y >= m) {
			return 1;
		}else if (board[x][y] == 'X') {
			return -1;
		}
		board[x][y] = 'X';
		int result = 0;
		for (int[] d : dir) {
			result = dfs(board, x + d[0], y + d[1]);
			if (result > 0) {
				return 1;
			}
		}
		return result;
	}
	@Test
	public void test() {
		char[][] a = {{'X', 'X', 'X', 'X'},
				{'X', 'O', 'O', 'X'},
				{'X', 'X', 'O', 'X'},
				{'X', 'O', 'X', 'X'}};
		solve(a);
		System.out.println(Arrays.deepToString(a));
	}
}
